import requests
from concurrent.futures import ThreadPoolExecutor
import time

# API 配置
API_URL = "http://localhost:5000/transcribe"
TEST_FILE = "1750303139_20250619111905A095.amr"  # 准备一个测试用的音频文件
CONCURRENT_REQUESTS = 2  # 并发请求数
REQUEST_COUNT = 20  # 总请求数

def send_request(file_path):
    try:
        with open(file_path, 'rb') as f:
            files = {'file': f}
            start_time = time.time()
            response = requests.post(API_URL, files=files)
            end_time = time.time()
            
            if response.status_code == 200:
                print(f"✅ 成功 | 耗时: {end_time - start_time:.2f}s | 响应: {response.text}")
            else:
                print(f"❌ 失败 | 状态码: {response.status_code} | 响应: {response.text}")
    except Exception as e:
        print(f"❌ 异常: {str(e)}")

def test_concurrency():
    print(f"开始并发测试，并发数: {CONCURRENT_REQUESTS}，总请求数: {REQUEST_COUNT}")
    
    with ThreadPoolExecutor(max_workers=CONCURRENT_REQUESTS) as executor:
        for _ in range(REQUEST_COUNT):
            executor.submit(send_request, TEST_FILE)

if __name__ == "__main__":
    test_concurrency()